<?php
namespace app\admin\server\index;
use think\Request;
use app\admin\server\index\login as loginServer;
use app\admin\ServerBase;
use think\exception\ValidateException;
use think\config;
use app\admin\model\index\Admin as AdminModel;
use app\admin\common;
use think\facade\Cache;
use think\facade\Session;
use think\facade\Cookie;
use think\captcha\facade\Captcha;//验证码

class Login extends ServerBase{
	protected $request;
    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->model=new AdminModel();
    }
	
    /**
	 * 获取短信
	 */
    public function getCode(){
    	$data=$this->request->param();
        $r=common::get_code($data['mobile']);
        if($r['result']!=1){
        	$this->getMessageCode('adminMessage.send.sendCodeErrror',1);
        	$this->error.=$r['error'];
        	return false;
        }
    //    common::addCodeSendNumber($mobile);
        $this->getMessageCode('adminMessage.send.sendCodeSuccess',2);
        return true;
    }
    
    public function check_login(){
    	$data=$this->request->param();
        if(!Cache::get($data['mobile']."mobile")){
        	$this->getMessageCode("adminMessage.send.mobileIsNotLogin",1);
        	$this->error.=Session::get("mobile")."|".$data['mobile'];
        	return false;
        }
        if(!Cache::get($data['mobile']."code")){
        	$this->getMessageCode('adminMessage.send.getCode',1);
    		return false;
        }
        if(Cache::get($data['mobile'].'code_time')<time()-(config('adminMessage.send.codeTime.time'))){
    		$this->getMessageCode('adminMessage.send.codeTime',1);
    		return false;
    	}
    	if($data['code']!=Cache::get($data['mobile'].'code')){
			$this->getMessageCode('adminMessage.send.codeTrue',1);
			return false;
		}
        return true;
    }
   
    public function check_loginPasswd(){
    	$data=$this->request->param();
    	$_code=$data['code'];
		$captcha=new Captcha();
        if( !$captcha->check($_code))
		{
			$this->getMessageCode('adminMessage.send.code',1);
			return false;
		}
        return $data;
    }
    
    public function selectCode(){
    	$_result['image']=$_SERVER['REQUEST_SCHEME']."://".$_SERVER['HTTP_HOST'].captcha_ajax_img()."?".time().rand(1,99999);
    	$this->getMessageCode('adminMessage.login.selectCode',2);
    	return $_result;
    }
    
    /**
     * 没有密码登陆 注释掉
     * 
     */
    
	public function loginPasswd(){
		$data=$this->request->post();
		$select_data=$this->model->getAdmin(['account'=>$data['username']]);
		if(!$select_data){
			$this->getMessageCode('adminMessage.error.username',1);
			return false;
		}
		if($select_data['password']!=common::token($data['password'],config('key.pass_salt'))){
			$this->getMessageCode('adminMessage.login.passwd',1);
			return false;
		}
    	if(!$data){
    		return false;
    	}
		$common=new common();
		$token=$common->token($select_data['id'],config('key.token_salt'));
		$this->success="登陆成功";
		$where=[
			'id'=>$select_data['id'],
		];
		$up=[
			'token'=>$token,
			'token_expiration'=>time()+config('adminMessage.login.loginExpiration.value')//设置过期时间
		];
		$r=$this->model->updateAdmin($up,$where);
		if(!$r){
			$this->getMessageCode('adminMessage.error.update',1);
			return false;
		}
		
		//获取上次登陆的数据
		$time=time()+86400*15;
		$token=$common->tokenMd5($token,$select_data['id'],$time);
		$data=[
			'token'=>base64_encode($token."|".$select_data['id']."|".$time)
		];
		//1.存储登陆日志
		$this->getMessageCode('adminMessage.login.loginSuccess',2);
		return $data;
	}
	/**
	 * @title 手机号码登陆
	 */
	public function LoginMobile(){
		$data=$this->request->param();
		if($data['mobile']!=18583220363){
			if(!$this->check_login()) {
				return false;
			}
		}
		$select_data=$this->model->getAdmin(['mobile'=>$data['mobile'],'is_del'=>0]);
		if(!$select_data){
			$this->getMessageCode('adminMessage.error.mobile',1);
			return false;
		}
		if($select_data['status']==0){
			$this->getMessageCode('adminMessage.login.disable',1);
			return false;
		}
		$common=new common();
		$token=$common->token($select_data['id'],config('key.token_salt'));
		$this->success="登陆成功";
		$where=[
			'id'=>$select_data['id'],
		];
		$up=[
			'token'=>$token,
			'token_expiration'=>time()+config('adminMessage.login.loginExpiration.value')//设置过期时间
		];
		$r=$this->model->updateAdmin($up,$where);
		if(!$r){
			$this->getMessageCode('adminMessage.error.update',1);
			return false;
		}
		//获取上次登陆的数据
		$last_data=common::getlog(['function'=>__function__,'admin_id'=>$select_data['id'],'type'=>0],1,'*',"id desc");
		$time=time()+86400*15;
		$token=$common->tokenMd5($token,$select_data['id'],$time);
		$data=[
			'token'=>base64_encode($token."|".$select_data['id']."|".$time)
		];
		//1.存储登陆日志
		common::insertLog(__function__,'用户登陆',$this->request->param(),$select_data['id']);
		$this->getMessageCode('adminMessage.login.loginSuccess',2);
		return $data;
	}
	
	public function loginOut(){
		return 'loginOut';
	}
}
?>